<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            @clear="getGoodLists"
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
          >
            <!-- - slot="append"，slot有插槽，还有位置的意思，这个属性的作用是：将button追加到input的后边
- 如果是slot="prepend"，那么就会将button放到input的前边 -->
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodLists"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4"
          ><el-button type="primary" @click="addPage"
            >添加商品</el-button
          ></el-col
        >
      </el-row>
      <!-- 表格列表区域  -->
      <el-table :data="goodList">
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="70"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140"
          ><template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template></el-table-column
        >
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="RemoveUserById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      goodList: [],
      dialogVisible: false,
    }
  },
  created() {
    this.getGoodLists()
  },
  methods: {
    // 跳转到添加页面
    addPage() {
      this.$router.push('/goods/add')
    }, // 获取参数列表
    async getGoodLists() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }

      // this.$message.success('获取数据成功')
      this.goodList = res.data.goods
      this.total = res.data.total
      console.log(this.goodList)
    }, // 分页条目数改变事件
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`)
      // 改变当前每页显示条目数
      this.queryInfo.pagesize = val
      //   重新调用getUserList()获取数据,刷新页面
      this.getGoodLists()
    }, // 分页页码值改变事件
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`)
      // 改变当前显示的页码值
      this.queryInfo.pagenum = val
      //   重新调用getUserList()获取数据,刷新页面
      this.getGoodLists()
    },
    // 删除按钮事件
    async RemoveUserById(id) {
      const removeResult = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err) // 捕捉错误
      // console.log(removeResult) // confirm | cancel
      if (removeResult !== 'confirm') {
        return this.$message.info('取消删除')
      }
      // 删除操作
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除用户失败')
      }
      this.$message.success('删除用户成功')
      this.getGoodLists()
    },
  },
}
</script>
<style lang="less" scoped>
</style>
